using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Xeml.Document;

namespace Xid.Controls
{
    public partial class AnnotationCtrl : UserControl
    {
        private XemlAnnotatableObject _annoObject;

        public XemlAnnotatableObject AnnotationObject
        {
            get { return _annoObject; }
            set { _annoObject = value; Setup(); }
        }

        private string fixTag;

        public string FixTag
        {
            get { return fixTag; }
            set
            {
                fixTag = value;
                if (!string.IsNullOrEmpty(fixTag))
                {
                    comboBoxTag.Text = fixTag;
                    comboBoxTag.Enabled = false;
                }
                else
                    comboBoxTag.Enabled = true;
            }
        }


        public AnnotationCtrl()
        {
            InitializeComponent();

            //comboBoxTag.SelectedValueChanged += new EventHandler(comboBoxTag_SelectedValueChanged);
            comboBoxTag.TextChanged += new EventHandler(comboBoxTag_TextChanged);
            comboBoxTag.SelectedValueChanged += new EventHandler(comboBoxTag_SelectedValueChanged);        
            //ObservationPoint op = new ObservationPoint();
            //op.Annotations.Add(new TaggedAnnotation("test", "Hallo Welt"));
            //op.Annotations.Add(new TaggedAnnotation("test2", "Hallo Welt2"));
            //this._annoObject = op;
            Setup();
            //textBoxAnnotation.Leave += new EventHandler(textBoxAnnotation_Leave);
            comboBoxTag.GotFocus += new EventHandler(comboBoxTag_GotFocus);
            //this.FixTag = "test";
        }


        private TaggedAnnotation _lastAnno = null;
        void comboBoxTag_SelectedValueChanged(object sender, EventArgs e)
        {
            _lastAnno = (TaggedAnnotation)comboBoxTag.SelectedItem;
            kryptonButton1.Visible = _lastAnno != null;
        }

        void comboBoxTag_GotFocus(object sender, EventArgs e)
        {
            if (comboBoxTag.SelectedItem != null && String.IsNullOrEmpty((comboBoxTag.SelectedItem as TaggedAnnotation).Annotation))
            {
                this.textBoxAnnotation.DataBindings.Clear();
                bl.Remove((comboBoxTag.SelectedItem as TaggedAnnotation));
                //bl.ResetBindings();
                comboBoxTag.SelectedItem = null;
                comboBoxTag.Text = "";

            }
        }




        private TaggedAnnotation _newAnno;
        void comboBoxTag_TextChanged(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(comboBoxTag.Text))
            {
     
                this.textBoxAnnotation.DataBindings.Clear();
                this.textBoxAnnotation.Text = "";
                textBoxAnnotation.Enabled = false;
                return;
            }
            else
                textBoxAnnotation.Enabled = true;


            if (!_annoObject.Annotations.Contains(comboBoxTag.Text))
            {
                this.textBoxAnnotation.GotFocus -= new EventHandler(textBoxAnnotation_GotFocus);
                this._newAnno = new TaggedAnnotation(comboBoxTag.Text);
                this.textBoxAnnotation.DataBindings.Clear();
                this.textBoxAnnotation.DataBindings.Add("Text", _newAnno, "Annotation");
                this.textBoxAnnotation.DataBindings[0].DataSourceUpdateMode = DataSourceUpdateMode.OnPropertyChanged;

                this.textBoxAnnotation.GotFocus += new EventHandler(textBoxAnnotation_GotFocus);
                kryptonLabel1.Values.Image = Properties.Resources.NewLogo;
            }
            else
            {


                if (comboBoxTag.SelectedItem == null)
                    comboBoxTag.SelectedItem = _annoObject.Annotations.Find(comboBoxTag.Text);

                this.textBoxAnnotation.DataBindings.Clear();
                object o = _annoObject.Annotations.Find(comboBoxTag.Text);
                this.textBoxAnnotation.DataBindings.Add("Text", comboBoxTag.SelectedItem, "Annotation");
                this.textBoxAnnotation.DataBindings[0].DataSourceUpdateMode = DataSourceUpdateMode.OnPropertyChanged;
                kryptonLabel1.Values.Image = null;
            }
        }

        void textBoxAnnotation_GotFocus(object sender, EventArgs e)
        {
            this.textBoxAnnotation.GotFocus -= new EventHandler(textBoxAnnotation_GotFocus);
            this._annoObject.Annotations.Add(_newAnno);
            bl.ResetBindings();
            if (comboBoxTag.Text == _newAnno.Tag)
            {
                kryptonLabel1.Values.Image = null;
            }
            comboBoxTag.SelectedItem = this._newAnno;
        }



        //void comboBoxTag_SelectedValueChanged(object sender, EventArgs e)
        //{
        //    this.textBoxAnnotation.DataBindings.Clear();
        //    if (comboBoxTag.SelectedItem != null)
        //    {
        //        this.textBoxAnnotation.DataBindings.Add("Text", comboBoxTag.SelectedItem, "Annotation");
        //        kryptonLabel1.Values.Image = null;
        //    }
        //}

        BindingList<TaggedAnnotation> bl;
        public void Setup()
        {
            if (_annoObject == null)
            {
                this.kryptonHeaderGroup1.Enabled = false;
                kryptonHeaderGroup1.ValuesSecondary.Heading = "null";
            }
            else
            {
                this.kryptonHeaderGroup1.Enabled = true;
                kryptonHeaderGroup1.ValuesSecondary.Heading = _annoObject.GetType().Name;

                bl = new BindingList<TaggedAnnotation>(_annoObject.Annotations);
                
                this.comboBoxTag.DisplayMember = "Tag";
                this.comboBoxTag.ValueMember = "Annotation";
                this.comboBoxTag.DataSource = bl;


                //this.textBoxAnnotation.DataBindings.Add("Text", comboBoxTag.SelectedItem, "Annotation");
                comboBoxTag_TextChanged(null, null);
            }
        }

        protected override void OnValidating(CancelEventArgs e)
        {
            base.OnValidating(e);
     
        }

        private void buttonSpecHeaderGroup1_Click(object sender, EventArgs e)
        {
            if (CloseMe != null)
                CloseMe(this, new EventArgs());
        }

        public event EventHandler CloseMe;

        private void kryptonButton1_Click(object sender, EventArgs e)
        {
            bl.Remove(_lastAnno);
            _lastAnno = null;
        }

    }
}
